package com.nari.ncps.screen.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.electric.ElectricRealTimeData;
import com.nari.ncps.screen.mapper.electric.ElectricOpermonEquipUseMapper;
import com.nari.ncps.screen.mapper.electric.ElectricRealTimeDataMapper;
import com.nari.ncps.screen.service.electric.ElectricOpermonEquipUseService;
import com.nari.ncps.screen.vo.electric.EquipmentUsageStatistics;
import com.nari.ncps.screen.entity.electric.ElectricOpermonEquipUse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 运行检测设备状态监测表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricOpermonEquipUseServiceImpl extends ServiceImpl<ElectricOpermonEquipUseMapper, ElectricOpermonEquipUse> implements ElectricOpermonEquipUseService {
    @Autowired
    ElectricRealTimeDataMapper electricRealTimeDataMapper;
    @Override
    public EquipmentUsageStatistics getEquipmentUsageStatistics(Integer year) {
        // 如果year为空，使用当前年份 qt_dp_electric_opermon_equip_use
        if (year == null) {
            year = java.time.LocalDate.now().getYear();
        }
//        获取当前时间的月份
        int month = java.time.LocalDate.now().getMonthValue();
//        获取当前月份的日期
        int day = java.time.LocalDate.now().getDayOfMonth();
//        设备状态监测-运行：获取实时数据
        LambdaQueryWrapper<ElectricRealTimeData> runQueryWrapper = new LambdaQueryWrapper<>();
        runQueryWrapper.eq(ElectricRealTimeData::getYear, year)
                .eq(ElectricRealTimeData::getMonth, month).eq(ElectricRealTimeData::getDay, day)
                .eq(ElectricRealTimeData::getDelFlag, 0).eq(ElectricRealTimeData::getDateType, 2)
                .orderByDesc(ElectricRealTimeData::getCreateTime)
                .last("LIMIT 1");

        ElectricRealTimeData realTimeData= electricRealTimeDataMapper.selectOne(runQueryWrapper);
        // 查询指定年份的所有数据
        LambdaQueryWrapper<ElectricOpermonEquipUse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ElectricOpermonEquipUse::getYear, year)
                .eq(ElectricOpermonEquipUse::getDelFlag, 0).eq(ElectricOpermonEquipUse::getDateType, 2)
                .orderByDesc(ElectricOpermonEquipUse::getCreateTime)
                .last("LIMIT 1");

        ElectricOpermonEquipUse equipment = this.getOne(queryWrapper);
        
        // 初始化统计数据
        EquipmentUsageStatistics statistics = new EquipmentUsageStatistics();
        statistics.setYear(year);
        if(realTimeData== null){
            statistics.setRunningUnits(0);
            statistics.setRunningCapacity(BigDecimal.ZERO);
        }else {
            statistics.setRunningUnits(realTimeData.getOperatingCapacity().intValueExact());
            statistics.setRunningCapacity(realTimeData.getNumberOfOperatingUnits());
        }
//        通过查询结果设置统计数据
        if (equipment == null ) {
            // 如果没有数据，返回0值
            statistics.setTotalUnits(0);
            statistics.setTotalInstalledCapacity(BigDecimal.ZERO);
            statistics.setStandbyUnits(0);
            statistics.setStandbyCapacity(BigDecimal.ZERO);
            statistics.setMaintenanceUnits(0);
            statistics.setMaintenanceCapacity(BigDecimal.ZERO);
            return statistics;
        }else {
            statistics.setTotalUnits(equipment.getTotalUnits());
            statistics.setTotalInstalledCapacity(equipment.getTotalInstalledCapacity());
            statistics.setStandbyUnits(equipment.getStandbyUnits());
            statistics.setStandbyCapacity(equipment.getStandbyCapacity());
            statistics.setMaintenanceUnits(equipment.getMaintenanceUnits());
            statistics.setMaintenanceCapacity(equipment.getTotalInstalledCapacity());
        }
        
        // 汇总所有电厂的设备数据（求和）
//        Integer totalUnits = equipmentList.stream()
//                .mapToInt(item -> item.getTotalUnits() != null ? item.getTotalUnits() : 0)
//                .sum();
//
//        BigDecimal totalInstalledCapacity = equipmentList.stream()
//                .map(item -> item.getTotalInstalledCapacity() != null ? item.getTotalInstalledCapacity() : BigDecimal.ZERO)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        Integer runningUnits = equipmentList.stream()
//                .mapToInt(item -> item.getRunningUnits() != null ? item.getRunningUnits() : 0)
//                .sum();
//
//        BigDecimal runningCapacity = equipmentList.stream()
//                .map(item -> item.getRunningCapacity() != null ? item.getRunningCapacity() : BigDecimal.ZERO)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        Integer standbyUnits = equipmentList.stream()
//                .mapToInt(item -> item.getStandbyUnits() != null ? item.getStandbyUnits() : 0)
//                .sum();
//
//        BigDecimal standbyCapacity = equipmentList.stream()
//                .map(item -> item.getStandbyCapacity() != null ? item.getStandbyCapacity() : BigDecimal.ZERO)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        Integer maintenanceUnits = equipmentList.stream()
//                .mapToInt(item -> item.getMaintenanceUnits() != null ? item.getMaintenanceUnits() : 0)
//                .sum();
//
//        BigDecimal maintenanceCapacity = equipmentList.stream()
//                .map(item -> item.getMaintenanceCapacity() != null ? item.getMaintenanceCapacity() : BigDecimal.ZERO)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        // 设置统计结果
//        statistics.setTotalUnits(totalUnits);
//        statistics.setTotalInstalledCapacity(totalInstalledCapacity);
//        statistics.setRunningUnits(runningUnits);
//        statistics.setRunningCapacity(runningCapacity);
//        statistics.setStandbyUnits(standbyUnits);
//        statistics.setStandbyCapacity(standbyCapacity);
//        statistics.setMaintenanceUnits(maintenanceUnits);
//        statistics.setMaintenanceCapacity(maintenanceCapacity);
        //总装机容量
//        statistics.setTotalInstalledCapacity(new BigDecimal("954.4"));
        
        return statistics;
    }

}
